import 'package:flutter_carplay/controllers/carplay_controller.dart';
import 'package:flutter_carplay/models/base_tab_bar_template.dart';
import 'package:flutter_carplay/models/button/bar_button.dart';
import 'package:flutter_carplay/models/list/list_section.dart';
import 'package:uuid/uuid.dart';

import 'list_constants.dart';
import 'list_item.dart';

/// A template object that displays and manages a list of items.
class CPListTemplate extends BaseTabBarTemplate {
  /// An array of list sections as [CPListSection], each can contain zero or more list items.
  List<CPListSection> sections;

  /// An optional array of title variants for the template’s empty view.
  /// Provide the strings as localized displayable content and order from most- to
  /// least- preferred. When there are no items in the list, the template displays
  /// an empty view with a title and a subtitle in place of the items. If you update
  /// the list and provide items, the template automatically removes the empty view.
  final List<String>? emptyViewTitleVariants;

  /// An optional array of subtitle variants for the template’s empty view.
  /// Provide the strings as localized displayable content and order from most- to
  /// least- preferred. When there are no items in the list, the template displays
  /// an empty view with a title and a subtitle in place of the items. If you update
  /// the list and provide items, the template automatically removes the empty view.
  final List<String>? emptyViewSubtitleVariants;

  /// Back button object
  final CPBarButton? backButton;

  ///右面按键
  List<CPBarButton>? nextButtons;

  /// Creates [CPListTemplate] to display a list of items, grouped into one or more sections.
  /// Each section contains an array of list items — objects that is [CPListItem]
  ///
  /// **Consider that some vehicles limit the number of items that [CPListTemplate] displays.**
  CPListTemplate({
    title,
    systemIcon,
    showsTabBadge,
    dispose,
    selectThis,
    required this.sections,
    this.emptyViewTitleVariants,
    this.emptyViewSubtitleVariants,
    this.backButton,
    this.nextButtons,
  }) : super(
            title: title,
            systemIcon: systemIcon,
            showsTabBadge: showsTabBadge,
            dispose: dispose,
            selectThis: selectThis);

  Map<String, dynamic> toJson() => {
        "_elementId": uniqueId,
        "title": title,
        "showsTabBadge": showsTabBadge,
        "systemIcon": systemIcon,
        "sections": sections.map((e) => e.toJson()).toList(),
        "emptyViewTitleVariants": emptyViewTitleVariants,
        "emptyViewSubtitleVariants": emptyViewSubtitleVariants,
        "backButton": backButton?.toJson(),
        "templateType": templateType,
        "nextButtons": nextButtons == null
            ? ""
            : nextButtons!.map((e) => e.toJson()).toList(),
      };

  void updateSections(List<CPListSection> sections) {
    this.sections = sections;
    FlutterCarPlayController.updateListTemplate(this);
  }

  void updateEmpty() {
    if (sections.isEmpty) {
      return;
    }
    sections = [];
    FlutterCarPlayController.updateListTemplate(this);
  }

  void updateLoading() {
    sections = [
      CPListSection(items: [
        CPListItem(
          text: "加载数据中...",
          accessoryType: CPListItemAccessoryTypes.none,
        ),
      ])
    ];
    FlutterCarPlayController.updateListTemplate(this);
  }

  void updateListItem(List<CPListItem> items) {
    if (sections.isEmpty) {
      sections = [CPListSection(items: items)];
    } else {
      sections[0].items = items;
    }
    if (items.isEmpty) {
      sections = [];
    }
    FlutterCarPlayController.updateListTemplate(this);
  }

  void setItems(List<CPListItem> items) {
    if (sections.isEmpty) {
      sections = [CPListSection(items: items)];
    } else {
      sections[0].items = items;
    }
    if (items.isEmpty) {
      sections = [];
    }
  }
}
